package managerBeanImpl;

import java.util.List;

import javax.ejb.Stateless;
import javax.faces.context.FacesContext;
import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
import javax.persistence.Query;

import managerBean.WishlistManagerBean;
import model.UtenteRegistrato;

@Stateless
public class WishlistManagerBeanImpl implements WishlistManagerBean{

	@PersistenceContext
    private EntityManager em;
	
	@Override
	public List getPacchettiMgr() {
		String username=FacesContext.getCurrentInstance().getExternalContext().getRemoteUser();
		Query query=em.createQuery("SELECT p FROM Pacchetto p,Wishlist w,UtenteRegistrato u WHERE u.username='"+username+"' and w.id.idWishlist=u.idWishlist and w.id.idPacchetto=p.idPacchetto");
		return query.getResultList();
	}

	@Override
	public void rimuoviPacchettoMgr(int idPacchetto) {
		String username=FacesContext.getCurrentInstance().getExternalContext().getRemoteUser();
		Query query=em.createQuery("SELECT u FROM UtenteRegistrato u WHERE u.username='"+username+"'");
		List<UtenteRegistrato> utente= query.getResultList();
		int idWishlist=utente.get(0).getIdWishlist();
		Query query2=em.createQuery("DELETE FROM Wishlist w WHERE w.id.idWishlist="+idWishlist+" and w.id.idPacchetto="+idPacchetto);
		query2.executeUpdate();
	}

	@Override
	public void rimuoviPacchettoRedirMgr(int idPacchetto, int idWishlist) {
		Query query=em.createQuery("DELETE FROM Wishlist w WHERE w.id.idWishlist="+idWishlist+" and w.id.idPacchetto="+idPacchetto);
		query.executeUpdate();
	}

}
